<?php

	include_once('meio.serverside.funcs.php');
	session_start();
	
	// get all vars from get and post. POST will overwrite GET
	// $_GET['firstWeekDay'] will become $firstWeekDay globaly, for example
	getVars($_GET, $_POST);
	
	// set locale to get the i18n week days, month names and abbreviated month names
	setlocale(LC_TIME, $i18n);
	
	$sessionKey = $classPrefix.$i18n.$firstWeekDay;
	if( !isset($_SESSION[$sessionKey]) ){
		// cache on session
		$i18n = getLocalNames();
		$_SESSION[$sessionKey] = serialize( $i18n );
	}
	else{
		// get cache on session
		$i18n = unserialize($_SESSION[$sessionKey]);
	}
	
	if(isset($m, $y)){
		// if there is as setted day, month and year
		$month = (int) $m;
		$year = (int) $y;
	}
	else{
		// get actual day
		$my = explode('-', date('n-Y'));
		$month = (int) $my[0];
		$year = (int) $my[1];
	}

	$nMonthDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
	$firstDayOfWeek = jddayofweek( gregoriantojd($month, 1, $year) );
	$firstWeekDayFromMonth = ($firstDayOfWeek - $firstWeekDay + 7) % 7;
	$numberOfWeeks = ceil(($nMonthDays+$firstWeekDayFromMonth)/7);
	$monthDay = 1;
?>

<?php if(isset($ft)): ?>
<input type="hidden" id="<?php echo $classPrefix; ?>month-abbrs" value='<?php echo json_encode($i18n['monthAbbrs']); ?>' />
<?php endif; ?>
<input type="hidden" id="<?php echo $classPrefix; ?>current-month" value="<?php echo $month ?>" />
<input type="hidden" id="<?php echo $classPrefix; ?>current-year" value="<?php echo $year ?>" />
<input type="hidden" id="<?php echo $classPrefix; ?>label" value="<?php echo $i18n['monthNames'][$month-1].', '.$year ?>" />

<table class="<?php echo $classPrefix; ?>month-table" cellpadding="0" cellspacing="2">
	
	<thead>
		<tr>
			<?php for($weekDay=0; $weekDay<7; $weekDay++): ?>
			<th><?php echo $i18n['weekDays'][($weekDay+$firstWeekDay)%7]; ?></th>
			<?php endfor; ?>
		</tr>
	</thead>
	
	<tbody>
		<?php $emptyTd = '<td class="'.$classPrefix.'nodate">&nbsp;</td>'; ?>
		<?php for($week=1; $week<=$numberOfWeeks; $week++): ?>
		<tr<?php if($week==1) echo ' class="'.$classPrefix.'firstrow"'; ?>>
			<?php 
				for($weekDay=1; $weekDay<=7; $weekDay++){
					$dateTd = '<td><a href="#">'.$monthDay.'</a></td>';
					if($monthDay > $nMonthDays || ($week == 1 && $weekDay > $firstWeekDayFromMonth) ){
						echo $dateTd; $monthDay++;
					}
					else{
						echo $emptyTd;
					}
				}
			?>
		</tr>
		<?php endfor; ?>
	</tbody>
	
</table>